package cn.xsly.ymcc.service.impl;

import cn.xsly.ymcc.constants.Constants;
import cn.xsly.ymcc.domain.*;
import cn.xsly.ymcc.dto.SmsDto;
import cn.xsly.ymcc.dto.UserRegisterDto;
import cn.xsly.ymcc.exception.GlobleException;
import cn.xsly.ymcc.fegin.LoginFeginClient;
import cn.xsly.ymcc.mapper.UserMapper;
import cn.xsly.ymcc.result.JSONResult;
import cn.xsly.ymcc.service.IUserAccountService;
import cn.xsly.ymcc.service.IUserBaseInfoService;
import cn.xsly.ymcc.service.IUserCodeService;
import cn.xsly.ymcc.service.IUserService;
import cn.xsly.ymcc.util.BitStatesUtils;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;


/**
 * <p>
 * 会员登录账号 服务实现类
 * </p>
 *
 * @author whale
 * @since 2022-10-19
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private LoginFeginClient loginFeginClient;
    @Autowired
    private IUserAccountService accountService;
    @Autowired
    private IUserBaseInfoService baseInfoService;
    @Autowired
    private IUserCodeService userCodeService;


    @GlobalTransactional
    @Override
    public void phoneRegister(UserRegisterDto registerDto) {
        String mobile = registerDto.getMobile();
        String password = registerDto.getPassword();
        String smsCode = registerDto.getSmsCode();
        //1.手机参数校验
        if (mobile.isEmpty() || password.isEmpty() || smsCode.isEmpty()) {
            throw new GlobleException("信息不能为空");
        }
        String smsKey = String.format(Constants.SMS_CODE, mobile);
        Object o = redisTemplate.opsForValue().get(smsKey);
        SmsDto codeDto = (SmsDto) redisTemplate.opsForValue().get(smsKey);
        System.out.println(codeDto);
        if (codeDto == null) {
            throw new GlobleException("手机验证码无效");
        }
        if (!smsCode.equals(codeDto.getSmsCode())) {
            throw new GlobleException("手机验证码不正确");
        }
        Wrapper<User> wrapper = new EntityWrapper<>();
        wrapper.eq("phone", mobile);
        User user = selectOne(wrapper);
        if (user != null) {
            throw new GlobleException("该手机已经被注册");
        }


        //2.保存登录信息和用户信息
        Login login = new Login();
        login.setUsername(mobile);
        login.setPassword(password);
        login.setType(1);
        login.setEnabled(true);
        JSONResult result = loginFeginClient.save(login);
        if (result.getData() == null || !result.isSuccess()) {
            throw new GlobleException("保存登录信息为空");
        }
        //为了保存用户数据，获取登录id
        Long loginId = Long.valueOf(result.getData().toString());

        user = new User();
        long time = new Date().getTime();
        user.setPhone(mobile);
        user.setCreateTime(time);
        user.setNickName(mobile);
        user.setLoginId(loginId);
        long addState = BitStatesUtils.addState(0l, BitStatesUtils.OP_REGISTED);
        addState = BitStatesUtils.addState(addState, BitStatesUtils.OP_ACTIVED);
        addState = BitStatesUtils.addState(addState, BitStatesUtils.OP_AUTHED_PHONE);
        user.setBitState(addState);
        insert(user);
        //System.out.println(1/0);

        //保存账户信息,userid必须同步
        accountService.save(user.getId());

        //保存基本信息--新增一个邀请用户码
        baseInfoService.save(user.getId(), registerDto.getRegChannel(), Long.valueOf(Constants.INVITE_USER));
    }

}
